package com.info33.platform.business.dialogue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.info33.platform.business.dialogue.dao.AiConversationDao;
import com.info33.platform.business.dialogue.entity.AiConversation;
import com.info33.platform.business.dialogue.service.AiConversationService;
import com.info33.platform.common.exception.BaseException;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.system.vo.SysUserVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author LF
 * @date 2023-10-16 10:09:15
 */
@Service
public class AiConversationServiceImpl extends ServiceImpl<AiConversationDao, AiConversation> implements AiConversationService {

    @Autowired
    private AiConversationDao aiConversationDao;

    @Override
    public Result<AiConversation> generateConversationGroup(SysUserVO currentUser) {
        if (ObjectUtils.isEmpty(currentUser)) {
            return Result.error("请登录!");
        }
        Long userId = currentUser.getSysTenantUserVO().getId();
        int count = count(new QueryWrapper<AiConversation>().eq("user_id", userId).eq("conversation_status", 0));
        if (count > 0) {
            return Result.error("已存在一个会话,关闭后可开启新的会话!");
        }
        AiConversation aiConversation = new AiConversation();
        aiConversation.setUserId(userId);
        aiConversation.setQuestionTime(new Date());
        if (!save(aiConversation)) {
            return Result.error("新建会话失败!");
        }
        return Result.ok(aiConversation);
    }

    @Override
    public Result<?> threadTest() {

        List<String> originalList = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");

        // 计算中间索引
        int midIndex = originalList.size() / 2;
        // 获取第一半部分
        List<String> firstHalf = originalList.subList(0, midIndex);
        // 获取第二半部分
        List<String> secondHalf = originalList.subList(midIndex, originalList.size());

        // 在接口内部创建一个新的线程
        Thread thread = new Thread(() -> {
            // 在这里编写需要在新线程中执行的代码逻辑
            // ...
            try {
                Thread.sleep(5000); // 休眠 5 秒
                aaa();

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println("异常一层!");
                for (String s : secondHalf) {
                    System.out.println("线程->后半:" + s);
                }
            }
        });

        // 启动线程
        thread.start();

        // 可以继续在原有线程中执行其他操作
        // ...
//        try {
//            Thread.sleep(5000); // 休眠 5 秒
//
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        for (String s : firstHalf) {
            System.out.println("线程->前:"+s);
        }

        return Result.ok("运行结束!");
    }

    public void aaa() throws BaseException {
        boolean A = true;
        if (A){
            bbb();
        }else {
            throw new BaseException("异常二层!");
        }
    }

    public void bbb() throws BaseException {
        boolean B = true;
        if (B){
            throw new BaseException("异常三层!");
        }
    }

}
